/*
 * MainWindow.cpp
 *
 *  Created on: 2012-8-28
 *      Author: yangentao@gmail.com
 */


#include "MainWindow.h"
#include "Cmd.h"

MainWindow::MainWindow() {
	setExitOnClose(true);

	Events.OnCommand = boost::bind(&MainWindow::onCommand, this, _1);
	Events.OnCreateChildren = boost::bind(&MainWindow::onCreateChildren, this);
	Events.OnControl = boost::bind(&MainWindow::onControl, this, _1, _2, _3);

	Super::setLayout(boost::shared_ptr<RelativeLayout>(new RelativeLayout()));
}

MainWindow::~MainWindow() {
}

HWND MainWindow::create(int x, int y, int width, int height) {
	setExitOnClose(true);
	Super::create(L"Hello", Xywh(x, y, width, height));
	Super::showWindow(SW_SHOW);
	Super::updateWindow();
	return Super::getWnd();
}

HMENU MainWindow::createMainMenu() {
	return ::createMainMenu();
}

void MainWindow::onCommand(UINT cmd) {
	if (cmd == CMD_FILE_NEW) {
		Log << "file.new" << endl;
	} else if (cmd == CMD_FILE_EXIT) {
		Log << "file.exit" << endl;
		Super::closeWindow();
	}
}

void MainWindow::onCreateChildren() {
	HMENU menu = createMainMenu();
	Super::setMenu(menu);

	Rect r = Super::getClientRect();
	r.zoom(-10);

	{
		boost::shared_ptr<RelativeParam> p(new RelativeParam());

		p->centerInParent = true;
//		p->alignParentLeft = true;
		p->width.fill();
		p->height.fill();
		p->setMargin(5);

		editor.setLayoutParam(p);
		editor.create(getWnd(), r);
	}

}

void MainWindow::onControl(UINT controlMsg, UINT controlID, HWND control) {
	Log << int(control) << " controlMsg:" << controlMsg << " ControlID:" << controlID << endl;
}
